const loadGruntTasks = require("load-grunt-tasks")
const browserSync = require('browser-sync')//开发服务器，支持热更新
const bs = browserSync.create()//创建服务器

const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}

module.exports = grunt => {
  loadGruntTasks(grunt)
  
  grunt.registerTask('bs-reload', function () {
    bs.reload()
  })

  grunt.initConfig({
    clean: {
      files: ['dist', 'temp']
    },
    eslint: {
      target: ['src/**/*.js']
    },
    sass: {
      options: {
        sourceMap: true, 
      },
      main: {
        files: [{
            expand: true,
            cwd: 'src',
            src: ['**/*.scss'],
            dest: 'dist',
            ext: '.css'
        }]
      }
    },
    babel: {
      options: {
        sourceMap: true,
        presets: ['@babel/preset-env']//需要转化哪些特性，env表示最新特性
      },
      main: {
        files: [{
          expand: true,
          cwd: 'src',
          src: ['**/*.js'],
          dest: 'dist',
          ext: '.js'
      }]
      }
    },
    swigtemplates: {
      options: {
        templatesDir: 'src',
      },
      main: { 
        context: data,
        src: "src/*.html",
        dest: "dist/"
      }
    },
    imagemin: {
      main: {
        files: [{
            expand: true,
            cwd: 'src',
            src: ['**/*.{png,jpg,gif}'],
            dest: 'dist'
        }]
      }
    },
    copy: {
      main: {
        files: [{
            expand: true, 
            cwd: 'public',
            src: '*', 
            dest: 'dist', 
          }]
      },
    },
    watch: {
      js: {
        files: ['src/**/*.js'],
        tasks: ['babel']//文件改变之后需要执行什么任务
      },
      css: {
        files: ['src/**/*.scss'],
        tasks: ['sass']
      },
      html: {
        files: ['src/**/*.html'],
        tasks: ['swigtemplates']
      },
      public: {
        files: ['src/assets/images/**',
        'src/assets/fonts/**',
        'public/**'],
      },
    },
    useminPrepare: {
      html: 'dist/*.html',
      options: {
        staging: 'temp',
        dest: 'dist',
        root: ['dist', '.'],
      }
    },
    usemin: {
      html: ['dist/*.html'],
      assetsDirs: ['dist']
    },
    htmlmin: { 
      options: { 
      removeComments: true,
      removeCommentsFromCDATA: true,
      collapseWhitespace: true,
      collapseBooleanAttributes: true,
      removeAttributeQuotes: true,
      removeRedundantAttributes: true,
      useShortDoctype: true,
      removeEmptyAttributes: true,
      removeOptionalTags: true
      },
      build:{ 
          expand: true,
          cwd: 'dist',
          src: ['**/*.html'],
          dest: 'dist',
      }
    },
    browserSync: {
      dev: {
        bsFiles: {
            src : [
                'dist/**/*.css',
                'dist/**/*.css',
                'dist/**/*.html'
            ]
        },
        options: {
            watchTask: true,
            notify: false,//不提示
            port: 2080,//指定端口
            server: {
              baseDir: ['dist', 'src', 'public'],
              routes: {//配置路由映射，仅用于开发环境，优先于baseDir
                '/node_modules': 'node_modules'//把/node_modules请求指向node_modules目录
              }
            }
        }
      }
    }
  })

  grunt.registerTask('lint', ['eslint'])
  grunt.registerTask('start', ['sass', 'babel', 'swigtemplates'])
  grunt.registerTask('serve', ['start', 'browserSync', 'watch'])
  grunt.registerTask('build', [
    'clean',
    'start', 
    'useminPrepare', 
    'concat:generated',
    'cssmin:generated',
    'uglify:generated',
    'usemin',
    'htmlmin',
    'imagemin',
    'copy'
  ])
}